/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#include "captchaDialog.h"

captchaDialog::captchaDialog(QString const& captcha, QString const& captchaDir, QString const& prefix, QString const& filetype)
{
    errorString = "";
    abort = false;
    waitingId = -1;

    setWindowTitle(tr("Captcha"));

    topLabel = new QLabel(tr("Please input captcha."));
    topLabel->setWordWrap(false);

    captchaLabel = new QLabel("");
    lineEditCaptcha = new QLineEdit("");
    okButton = new QPushButton(tr("OK"));
    okButton->setDefault(true);
    layout = new QGridLayout;

    layout->addWidget(topLabel, 0, 0, 1, 3);
    layout->addWidget(captchaLabel, 1, 0);
    layout->addWidget(lineEditCaptcha, 1, 1);
    layout->addWidget(okButton, 1, 2);

    setLayout(layout);

    http = new QHttp;

    connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(slot_http_requestFinished(int, bool)));
    connect(http, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(slot_http_sslErrors(const QList<QSslError> &)));

    connect(okButton, SIGNAL(clicked(bool)), this, SLOT(accept()));

    captchaFile = new QTemporaryFile(captchaDir + QDir::separator() + prefix + "_XXXXXX." + filetype);
    captchaFile->setAutoRemove(false);
    if(captchaFile->open())
    {
        QUrl url(captcha);

        QHttpRequestHeader header("GET", url.path());
        header.setValue("Host", url.host());

        if(captcha == "https")
        {
            http->setHost(url.host(), QHttp::ConnectionModeHttps);
        } else
        {
            http->setHost(url.host());
        }

        waitingId = http->request(header, QByteArray(), captchaFile);
    } else
    {
        errorString = tr("Cannot create captcha file.");
    }
}

captchaDialog::~captchaDialog()
{
    abort = true;

    if(captchaFile)
    {
        if(captchaFile->isOpen())
            captchaFile->close();

        delete captchaFile;
    }

    if(http)
    {
        http->close();
        delete http;
    }

    if(layout) delete layout;

    if(topLabel) delete topLabel;
    if(captchaLabel) delete captchaLabel;
    if(lineEditCaptcha) delete lineEditCaptcha;
    if(okButton) delete okButton;
}

void captchaDialog::slot_http_sslErrors(const QList<QSslError> & errors)
{
    //TODO: ssl errors
    Q_UNUSED(errors);
    http->ignoreSslErrors();
}

void captchaDialog::slot_http_requestFinished(int id, bool error)
{
    Q_UNUSED(error);

    if(abort)
    {
        if(captchaFile)
        {
            if(captchaFile->isOpen()) captchaFile->close();
            errorString = http->errorString();
        }

        return;
    }

    if(waitingId != id) return;

    if(captchaFile) captchaFile->close();

    captchaLabel->setPixmap(QPixmap(captchaFile->fileName()));
}
